package com.climber.android.commonres.ui.dialog

import android.os.Bundle
import android.text.InputFilter
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.BaseDialogFragment
import com.climber.android.commonres.R
import io.ganguo.library.mvp.util.ActivityUtils
import io.ganguo.library.mvp.util.KeyboardUtils
import io.ganguo.library.mvp.util.StringUtils
import io.ganguo.library.mvp.util.UIUtils

/**
 * Created by Mikes at 2019-11-08 18:26
 */
class CommonInputActionDialog : BaseDialogFragment(){
    companion object {
        fun generate(body: CommonInputActionDialog.() -> CommonInputActionDialog): CommonInputActionDialog {
            return with(CommonInputActionDialog()) {
                body()
            }
        }
    }

    private var title = ""
    private var titleTextColor = UIUtils.getColor(R.color.common_text_primary_dark)
    private var titleTextSize = 17F

    private var contentHint = ""
    private var preContent = ""
    private var contentTextColor = UIUtils.getColor(R.color.common_text_primary)
    private var contentTextSize = 15F
    private var contentLength = 0

    private var cancelBtnLabel = ""
    private var cancelBtnColor = UIUtils.getColor(R.color.common_text_primary)

    private var confirmBtnLabel = ""
    private var confirmBtnColor = UIUtils.getColor(R.color.common_text_primary)

    private var cancelBtnCallBack: (() -> Unit)? = null
    private var confirmBtnCallBack: ((String) -> Unit)? = null

    fun setTitle(title: String) {
        this.title = title
    }

    fun setTitleTextSize(textSize : Float){
        this.titleTextSize = textSize
    }

    fun setTitleTextColor(textColor : Int){
        this.titleTextColor = textColor
    }

    fun setContentHint(contentHint: String) {
        this.contentHint = contentHint
    }

    fun setPreContent(preContent : String){
        this.preContent = preContent;
    }

    fun setContentTextSize(textSize : Float){
        this.contentTextSize = textSize
    }

    fun setContentTextColor(textColor : Int){
        this.contentTextColor = textColor
    }

    fun setContentLength(length : Int){
        this.contentLength = length
    }

    fun setCancelBtnLabel(label: String, callback: (() -> Unit)? = null) {
        this.cancelBtnLabel = label
        this.cancelBtnCallBack = callback
    }

    fun setCancelBtnColor(btnColor : Int) {
        this.cancelBtnColor = btnColor
    }

    fun setConfirmBtnLabel(label: String, callback: ((String) -> Unit)? = null) {
        this.confirmBtnLabel = label
        this.confirmBtnCallBack = callback
    }

    fun setConfirmBtnColor(btnColor : Int) {
        this.confirmBtnColor = btnColor
    }

    fun create(): CommonInputActionDialog {
        val appDialog = CommonInputActionDialog()

        val bundle = Bundle()
        bundle.putString("title", this.title)
        bundle.putFloat("titleTextSize", this.titleTextSize)
        bundle.putInt("titleTextColor", this.titleTextColor)

        bundle.putString("contentHint", this.contentHint)
        bundle.putString("preContent", this.preContent)
        bundle.putFloat("contentTextSize", this.contentTextSize)
        bundle.putInt("contentTextColor", this.contentTextColor)
        bundle.putInt("contentLength", this.contentLength)

        bundle.putString("cancelBtnLabel", this.cancelBtnLabel)
        bundle.putInt("cancelBtnColor", this.cancelBtnColor)

        bundle.putString("confirmBtnLabel", this.confirmBtnLabel)
        bundle.putInt("confirmBtnColor", this.confirmBtnColor)

        appDialog.arguments = bundle

        appDialog.cancelBtnCallBack = cancelBtnCallBack
        appDialog.confirmBtnCallBack = confirmBtnCallBack

        return appDialog
    }

    private lateinit var tvDialogTitle: TextView
    private lateinit var etDialogBody: EditText

    private lateinit var cancelBtn: TextView
    private lateinit var confirmBtn: TextView

    override fun getLayoutResId(): Int = R.layout.com_dialog_input_with_actions

    override fun initView(dialogView: View) {
        tvDialogTitle = dialogView.findViewById(R.id.tvDialogTitle) as TextView
        etDialogBody = dialogView.findViewById(R.id.etDialogBody) as EditText

        cancelBtn = dialogView.findViewById<TextView>(R.id.cancelBtn)
        confirmBtn = dialogView.findViewById<TextView>(R.id.confirmBtn)

        cancelBtn.setOnClickListener {
            dismiss()
            cancelBtnCallBack?.invoke()
        }

        confirmBtn.setOnClickListener {
            dismiss()
            KeyboardUtils.hideSoftInput(ActivityUtils.getTopActivity())
            confirmBtnCallBack?.invoke(etDialogBody.text.toString())
        }

        dialog?.run {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
        }
    }

    override fun initData() {
        val tempArguments = arguments
        if (tempArguments == null) {
            dismiss()
            return
        }

        setAnim(R.style.DialogScaleIn)

        val title = tempArguments.getString("title", "")
        if(StringUtils.isEmpty(title)){
            tvDialogTitle.visibility = View.GONE
        } else{
            tvDialogTitle.text = title
        }

        tempArguments.getFloat("titleTextSize").run {
            tvDialogTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, this)
        }

        tempArguments.getInt("titleTextColor").run {
            tvDialogTitle.setTextColor(this)
        }

        val contentHint = tempArguments.getString("contentHint", "")
        etDialogBody.hint = contentHint

        val preContent = tempArguments.getString("preContent", "")
        if(preContent.isNotEmpty()){
            etDialogBody.setText(preContent)
        }

        tempArguments.getFloat("contentTextSize").run {
            etDialogBody.setTextSize(TypedValue.COMPLEX_UNIT_SP, this)
        }

        tempArguments.getInt("contentTextColor").run {
            etDialogBody.setTextColor(this)
        }

        tempArguments.getInt("contentLength").run {
            if(this > 0){
                etDialogBody.filters = arrayOf(InputFilter.LengthFilter(this))
                etDialogBody.setSingleLine(true)
            }
        }

        val cancelBtnLabel = tempArguments.getString("cancelBtnLabel")
        if(StringUtils.isNotEmpty(cancelBtnLabel)){
            cancelBtn.text = cancelBtnLabel
        } else{
            cancelBtn.visibility = View.GONE
        }

        val confirmBtnLabel = tempArguments.getString("confirmBtnLabel")
        if(StringUtils.isNotEmpty(confirmBtnLabel)){
            confirmBtn.text = confirmBtnLabel
        } else{
            confirmBtn.visibility = View.GONE
        }

        if(StringUtils.isEmail(cancelBtnLabel)
            && StringUtils.isEmail(confirmBtnLabel)){
            cancelBtn.visibility = View.GONE
            confirmBtn.visibility = View.GONE
        }
    }

    override fun setSize(width: Int, height: Int) {
        super.setSize(UIUtils.getDisplayWidth() - UIUtils.dip2Px(30) * 2, height)
    }

    override fun setLocation(gravity: Int) {
        super.setLocation(Gravity.CENTER)
    }
}